using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class OtherColorsSubMenu : Script
{
	public enum OtherColors
	{
		Eye,
		Hair
	}

	public static UIMenu menu = new UIMenu("", TextSetting.ocs_Menu, new Point(0, -107));

	private static List<dynamic> colorList = new List<object>(new string[1] { string.Empty });

	private static UIMenuListItem colorItem;

	private static UIMenuListItem highlightColorItem;

	private static OtherColors currentSetType;

	public OtherColorsSubMenu()
	{
		Main.pool.Add(menu);
		menu.OnListChange += OnListChange;
		menu.OnItemSelect += OnItemSelect;
		menu.OnMenuClose += OnMenuClose;
	}

	public static void InitOtherColorsSubMenu(OtherColors setType)
	{
		currentSetType = setType;
		menu.Clear();
		colorList = new List<object>();
		Skin.OtherColors otherColors = ((DressingScript.Dressing != Main.Member.P2) ? Main.P1.skin.Other_Colors : Main.P2.skin.Other_Colors);
		if (currentSetType == OtherColors.Eye)
		{
			for (int i = 0; i < Skin.OTHER_COLOR_EYE_NUM; i++)
			{
				colorList.Add(i);
			}
			colorItem = new UIMenuListItem(TextSetting.ocs_ListItem_EyeColor, colorList, otherColors.eyeColorId);
			menu.AddItem(colorItem);
		}
		else if (currentSetType == OtherColors.Hair)
		{
			for (int j = 0; j < Skin.OTHER_COLOR_HAIR_NUM; j++)
			{
				colorList.Add(j);
			}
			colorItem = new UIMenuListItem(TextSetting.ocs_ListItem_HairColor, colorList, otherColors.hairColorId);
			highlightColorItem = new UIMenuListItem(TextSetting.ocs_ListItem_HairHighlightColor, colorList, otherColors.hairHighlightColorID);
			menu.AddItem(colorItem);
			menu.AddItem(highlightColorItem);
		}
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.ocs_MenuItem_RandomizeColor));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
		menu.RefreshIndex();
	}

	private static void UpdateOtherColorsValue(OtherColors setType)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			switch (setType)
			{
			case OtherColors.Eye:
				Main.P2.skin.Other_Colors.eyeColorId = colorItem.Index;
				break;
			case OtherColors.Hair:
				Main.P2.skin.Other_Colors.hairColorId = colorItem.Index;
				Main.P2.skin.Other_Colors.hairHighlightColorID = highlightColorItem.Index;
				break;
			}
			return;
		}
		switch (setType)
		{
		case OtherColors.Eye:
			Main.P1.skin.Other_Colors.eyeColorId = colorItem.Index;
			Main.Clone.skin.Other_Colors.eyeColorId = colorItem.Index;
			break;
		case OtherColors.Hair:
			Main.P1.skin.Other_Colors.hairColorId = colorItem.Index;
			Main.P1.skin.Other_Colors.hairHighlightColorID = highlightColorItem.Index;
			Main.Clone.skin.Other_Colors.hairColorId = colorItem.Index;
			Main.Clone.skin.Other_Colors.hairHighlightColorID = highlightColorItem.Index;
			break;
		}
	}

	private void OnListChange(UIMenu sender, UIMenuListItem list, int index)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			if (currentSetType == OtherColors.Eye)
			{
				SkinHelper.SetPedEyeColor(Main.P2.Character, colorItem.Index);
				Main.P2.skin.Other_Colors.eyeAction = true;
			}
			else if (currentSetType == OtherColors.Hair)
			{
				SkinHelper.SetPedHairColor(Main.P2.Character, colorItem.Index, highlightColorItem.Index);
				Main.P2.skin.Other_Colors.hairAction = true;
			}
		}
		else if (currentSetType == OtherColors.Eye)
		{
			SkinHelper.SetPedEyeColor(Main.P1.Character, colorItem.Index);
			SkinHelper.SetPedEyeColor(Main.Clone.Character, colorItem.Index);
			Main.P1.skin.Other_Colors.eyeAction = true;
			Main.Clone.skin.Other_Colors.eyeAction = true;
		}
		else if (currentSetType == OtherColors.Hair)
		{
			SkinHelper.SetPedHairColor(Main.P1.Character, colorItem.Index, highlightColorItem.Index);
			SkinHelper.SetPedHairColor(Main.Clone.Character, colorItem.Index, highlightColorItem.Index);
			Main.P1.skin.Other_Colors.hairAction = true;
			Main.Clone.skin.Other_Colors.hairAction = true;
		}
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		if (currentSetType == OtherColors.Eye)
		{
			switch (index)
			{
			case 1:
			{
				Random random = new Random(Guid.NewGuid().GetHashCode());
				colorItem.Index = random.Next(0, colorList.Count);
				if (DressingScript.Dressing == Main.Member.P2)
				{
					SkinHelper.SetPedEyeColor(Main.P2.Character, colorItem.Index);
					Main.P2.skin.Other_Colors.eyeAction = true;
					break;
				}
				SkinHelper.SetPedEyeColor(Main.P1.Character, colorItem.Index);
				SkinHelper.SetPedEyeColor(Main.Clone.Character, colorItem.Index);
				Main.P1.skin.Other_Colors.eyeAction = true;
				Main.Clone.skin.Other_Colors.eyeAction = true;
				break;
			}
			case 2:
				DressingScript.Distance();
				break;
			case 3:
				DressingScript.TurnLeft();
				break;
			case 4:
				DressingScript.TurnRight();
				break;
			case 5:
				DressingScript.AngleUp();
				break;
			case 6:
				DressingScript.AngleDown();
				break;
			}
		}
		else
		{
			if (currentSetType != OtherColors.Hair)
			{
				return;
			}
			switch (index)
			{
			case 2:
			{
				Random random2 = new Random(Guid.NewGuid().GetHashCode());
				colorItem.Index = random2.Next(0, colorList.Count);
				highlightColorItem.Index = random2.Next(0, colorList.Count);
				if (DressingScript.Dressing == Main.Member.P2)
				{
					SkinHelper.SetPedHairColor(Main.P2.Character, colorItem.Index, highlightColorItem.Index);
					Main.P2.skin.Other_Colors.hairAction = true;
					break;
				}
				SkinHelper.SetPedHairColor(Main.P1.Character, colorItem.Index, highlightColorItem.Index);
				SkinHelper.SetPedHairColor(Main.Clone.Character, colorItem.Index, highlightColorItem.Index);
				Main.P1.skin.Other_Colors.hairAction = true;
				Main.Clone.skin.Other_Colors.hairAction = true;
				break;
			}
			case 3:
				DressingScript.Distance();
				break;
			case 4:
				DressingScript.TurnLeft();
				break;
			case 5:
				DressingScript.TurnRight();
				break;
			case 6:
				DressingScript.AngleUp();
				break;
			case 7:
				DressingScript.AngleDown();
				break;
			}
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		UpdateOtherColorsValue(currentSetType);
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
